package pl.plesken.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import pl.plesken.languages.SetLanguage;

public class Search extends Dialog {

	private Text findText;
	protected Object result;
	protected Shell shell;
	private String word;

	
	
	public String getWord() {
		return word;
	}

	public void setWord(String word) {
		this.word = word;
	}

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public Search(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public Search(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new GridLayout());
		shell.setSize(364, 147);
		shell.setText(SetLanguage.getString("Search.appName")); //$NON-NLS-1$

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 2;
		composite.setLayout(gridLayout_1);

		final Label findLabel = new Label(composite, SWT.NONE);
		final GridData gd_findLabel = new GridData(SWT.FILL, SWT.CENTER, false, true);
		gd_findLabel.widthHint = 130;
		findLabel.setLayoutData(gd_findLabel);
		findLabel.setText(SetLanguage.getString("Search.word")); //$NON-NLS-1$

		findText = new Text(composite, SWT.BORDER);
		final GridData gd_findText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		findText.setLayoutData(gd_findText);

		final Group group = new Group(shell, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		group.setLayout(gridLayout);

		final Button findButton = new Button(group, SWT.NONE);
		findButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				finder();
			}
		});
		final GridData gd_findButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_findButton.widthHint = 100;
		findButton.setLayoutData(gd_findButton);
		findButton.setText(SetLanguage.getString("Search.find")); //$NON-NLS-1$

		final Button cancelButton = new Button(group, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.close();
			}
		});
		final GridData gd_cancelButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_cancelButton.widthHint = 100;
		cancelButton.setLayoutData(gd_cancelButton);
		cancelButton.setText(SetLanguage.getString("Search.cancel")); //$NON-NLS-1$
		//
	}

	private void finder() {
		word = findText.getText();
		shell.close();
	}
	
}
